﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Threading;
using System.Globalization;
using System.Xml.Linq;

namespace ES_Project.Pages
{

    public partial class WebForm20 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
       void clear()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
    }
        protected void GridView1_RowDataBound(Object s, GridViewRowEventArgs e)
        {

            HyperLink st_code = (HyperLink)(e.Row.FindControl("st_code"));
            if (st_code != null)
            {


                st_code.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));
               
            }
            HyperLink st_name = (HyperLink)(e.Row.FindControl("st_name"));
            if (st_name != null)
            {


                st_name.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));

            }
            HyperLink st_gender = (HyperLink)(e.Row.FindControl("st_gender"));
            if (st_gender != null)
            {


                st_gender.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));

            }
            HyperLink st_school = (HyperLink)(e.Row.FindControl("st_school"));
            if (st_school != null)
            {


                st_school.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));

            }
            HyperLink st_birthday = (HyperLink)(e.Row.FindControl("st_birthday"));
            if (st_birthday != null)
            {


                st_birthday.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));

            }
            HyperLink register_date = (HyperLink)(e.Row.FindControl("register_date"));
            if (register_date != null)
            {


                register_date.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));

            }
            HyperLink st_end_date = (HyperLink)(e.Row.FindControl("st_end_date"));
            if (st_end_date != null)
            {


                st_end_date.NavigateUrl = "EditStudent.aspx?st_id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "st_id"));

            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            BindStudent();


        }
        void BindStudent()
        {
            string condi = "where 1=1";
            if (!TextBox1.Text.Equals(""))
            {

                condi = condi + " and (  st_code ='" + TextBox1.Text + "')";
            }
            if (TextBox2.Text.Trim() != "" && TextBox3.Text.Trim() != "")
            {

                condi = condi + " and (  register_date between '" + Convert.ToDateTime(TextBox2.Text).AddYears(-543).ToString("yyyy-MM-dd") + "' and '" + Convert.ToDateTime(TextBox3.Text).AddYears(-543).ToString("yyyy-MM-dd") + "')";

            }

            if (TextBox4.Text.Trim() != "" && TextBox5.Text.Trim() != "")
            {
                condi = condi + " and (  st_end_date between '" + Convert.ToDateTime(TextBox4.Text).AddYears(-543).ToString("yyyy-MM-dd") + "' and '" + Convert.ToDateTime(TextBox5.Text).AddYears(-543).ToString("yyyy-MM-dd") + "')";
            }
            MySqlConnection Conn = new MySqlConnection(StrConn);

            string sqlsm;


            sqlsm = "select * from student_mast " + condi;

            //sqlsm = "SELECT * FROM Student_Mast inner join student_detail on Student_Mast.st_id = student_detail.st_id "+ condi;
            Conn.Open();
            MySqlDataAdapter da = new MySqlDataAdapter(sqlsm, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Student_Mast");
            GridView1.DataSource = ds.Tables["Student_Mast"];
            GridView1.DataBind();

            if (ds.Tables[0].Rows.Count == 0)
            {
                GridView1.DataSource = null;
                GridView1.DataBind();



                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ReturnMessinit1", "alert('ไม่พบข้อมูล');", true);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            clear();
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

           
            GridView1.PageIndex = e.NewPageIndex;
            BindStudent();
            GridView1.DataBind();
        }
    }
}